package cn.wolfcode.education.service.impl;

import cn.wolfcode.education.domain.LossStu;
import cn.wolfcode.education.domain.RegularStu;
import cn.wolfcode.education.domain.SystemDictionaryItem;
import cn.wolfcode.education.mapper.LossStuMapper;
import cn.wolfcode.education.mapper.RegularStuMapper;
import cn.wolfcode.education.query.QueryObject;
import cn.wolfcode.education.query.RegularStuQuery;
import cn.wolfcode.education.service.IRegularStuService;
import cn.wolfcode.education.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class RegularStuServiceImpl implements IRegularStuService {

    @Autowired
    private RegularStuMapper regularStuMapper;
    @Autowired
    private LossStuMapper lossStuMapper;


    //保存方法的实现
    public void insert(RegularStu regularStu) {
        regularStuMapper.insert(regularStu);
    }

    //更新方法的实现
    public void update(RegularStu regularStu) {
        regularStuMapper.updateByPrimaryKey(regularStu);
    }

    //查询方法的实现
    public PageResult query(RegularStuQuery qo) {
        Integer total = regularStuMapper.queryForCount(qo);
        if (total == 0) {
            return new PageResult();
        }
        List<RegularStu> rows = regularStuMapper.queryForList(qo);
        return new PageResult(total, rows);
    }

    //退学方法的实现
    public void removeStu(Long id) {
        //将当前学员的状态设置为退学状态
        regularStuMapper.removeStu(id);
        //通过id从正式学员中获取当前学员的信息
        RegularStu regularStu = regularStuMapper.selectByPrimaryKey(id);
        System.out.println(regularStu);
        //将学员退学管理中所需的字段设置进退学学员对象中
        LossStu record = new LossStu();
        //设置退学学员的正式员工表中的id值
        record.setRegularStuId(id);
        //设置退学学员的姓名
        record.setName(regularStu.getName());
        //设置退学学员的联系电话
        record.setTel(regularStu.getTel());
        //设置退学学员的流失阶段
        record.setClassStatus(regularStu.getClassStatus());
        //设置退学学员的退学时间
        Date removeDate = new Date();
        record.setRemoveStuTime(removeDate);
        Date inClassDate = null;
        if (regularStu.getClassStatus() == 0) {
            //获取当前学员的入学时间(基础班的入学时间)
            inClassDate = regularStu.getInClassTime();
        } else {
            //获取当前学员的升班事件(大神班的入学时间)
            inClassDate = regularStu.getClassPromoteDate();
        }
        //设置退学学员的上课天数
        record.setInClassDays(String.valueOf((removeDate.getTime() - inClassDate.getTime()) / (1000 * 60 * 60 * 24)));
        //设置是否退款
        record.setRefund(false);
        //设置营销人员
        record.setInputEmployee(regularStu.getInputEmployee());
        //设置经办人

        //设置审核状态
        record.setAudit(0);
        lossStuMapper.insert(record);
    }

    //休学方法的实现
    public void restStu(Long id) {
        regularStuMapper.restStu(id);
    }

    //恢复正常方法的实现
    public void recoverStu(Long id) {
        regularStuMapper.recoverStu(id);
    }

    //升班方法的实现
    public void promote(Long id, int status) {
        if (status != 0) {
            throw new RuntimeException("不具备升班条件");
        }
        Date classPromoteDate = new Date();
        //调用升班方法
        regularStuMapper.promote(id, classPromoteDate);
    }

    //毕业方法的实现
    public void endRegularStu(Long id) {
        regularStuMapper.endRegularStu(id);
    }

    //留级方法的实现
    public void retentionRegularStu(Long id, Long classgradeId) {
        regularStuMapper.retentionRegularStu(id, classgradeId);
    }

    //查询数据字典中的校区方法的实现
    public List<SystemDictionaryItem> selectAllCampusBySystemDictionary() {
        return regularStuMapper.selectCampusBySystemDictionary();
    }

    //查询数据字典中的学院方法的实现
    public List<SystemDictionaryItem> selectAllCollegeBySystemDictionary() {
        return regularStuMapper.selectAllCollegeBySystemDictionary();
    }

    //查询数据字典中的支付类型方法的实现
    public List<SystemDictionaryItem> selectAllPaymentType() {
        return regularStuMapper.selectAllPaymentType();
    }

    //查询数据字典中的付款方式方法的实现
    public List<SystemDictionaryItem> selectAllPayment() {
        return regularStuMapper.selectAllPayment();
    }

    //查询正式学员报表方法的实现
    public List<Map<String, Object>> queryForRegularStuChart(QueryObject qo) {
        return regularStuMapper.selectForRegularStuChart(qo);
    }

    // 查询某个学员是否存在
    public void isExsists(String stuName) {
        // 获取到所有学生
        List<Map<Long, Object>> mapList = regularStuMapper.getAllStu();
        int sum = 0;

        for (Map<Long, Object> map : mapList) {
            Collection<Object> values = map.values();
            for (Object value : values) {
                if (stuName.equals(value)) {
                    sum++;
                }
            }
            if (sum == 0) {
                throw new RuntimeException("不存在该学员哦,亲!点击确定仍可提交");
            }
        }
    }
}
